package com.x.epspace.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.x.epspace.common.AppConfig;
import com.x.epspace.common.HttpRequestor;
import com.x.epspace.common.Tool;
import com.x.epspace.dao.BindEntDao;
import com.x.epspace.entity.BindEnt;
import com.x.epspace.entity.BindPaper;
import com.x.epspace.ext.HandleSessionService;

@Service("bindPaperService")
public class BindPaperService {
	
	private static final Log log = LogFactory.getLog(BindPaperService.class);
	
	@Resource
	private HandleSessionService handleSession;
	
	@Resource
	private BindEntDao bindEntDao;
	
	/**
	 * 获取当前登录用户的加密档案云身份信息
	 * @return
	 */
	public Map<String,String> getUserEntBindPaper(){
		//初始化返回
		Map<String, String> returnMap = new HashMap<String, String>();
		//获取session中的登录企业信息
		Object bindEntObj = handleSession.getVal(AppConfig.getProperty("ENT_SESSION_KEY","SESSION_KEY"));
		if(bindEntObj == null){
			returnMap.put("result",
					AppConfig.getProperty("fail", "return_code"));
			returnMap.put("description", "未获取到用户登录企业身份信息");
			return returnMap;
		}
		BindEnt bindEnt = (BindEnt)bindEntObj;
		BindPaper bindPaper = bindEnt.getBindPaper();
		if(bindPaper != null){
			returnMap.put("result",
					AppConfig.getProperty("success", "return_code"));
			returnMap.put("description", "成功获取档案云身份");
			returnMap.put("params", genLoginEncParam(bindPaper));
			return returnMap;
		}
		//还未绑定档案云身份，调用远程方法注册档案云相关身份
		String regPaperParams = genRegPaperEncParam(bindEnt);
		String invokeUrl = AppConfig.getProperty("regPaperUrl", "system");
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("params", regPaperParams);
		try {
			String returnJson = HttpRequestor.doPost(invokeUrl, paramMap);
			if(!Tool.isNotNullOrEmpty(returnJson)){
				returnMap.put("result",
						AppConfig.getProperty("fail", "return_code"));
				returnMap.put("description", "获取档案云身份出错");
				return returnMap;
			}
			JSONObject json=JSONObject.fromObject(returnJson);
			String result = json.getString("result");
			if(!result.equals(AppConfig.getProperty("success", "return_code"))){
				returnMap.put("result",
						AppConfig.getProperty("fail", "return_code"));
				returnMap.put("description", "获取档案云身份出错");
				return returnMap;
			}
			//正确获取档案云身份，保存档案云身份
			JSONObject bindPaperObj = json.getJSONObject("bindPaper");
			String loginName = bindPaperObj.getString("loginName");
			String encPwd = bindPaperObj.getString("encPwd");
			bindPaper = new BindPaper();
			bindPaper.setLoginName(loginName);
			bindPaper.setEncPwd(encPwd);
			bindPaper.setBindTime(System.currentTimeMillis());
			bindEnt.setBindPaper(bindPaper);
			bindEntDao.save(bindEnt);
			returnMap.put("result",
					AppConfig.getProperty("success", "return_code"));
			returnMap.put("description", "成功获取档案云身份");
			returnMap.put("params", genLoginEncParam(bindPaper));
			return returnMap;
		} catch (Exception e) {
			log.error(e);
			returnMap.put("result",
					AppConfig.getProperty("fail", "return_code"));
			returnMap.put("description", "获取档案云身份出错");
			return returnMap;
		}
		
	}
	
	private String genLoginEncParam(BindPaper bindPaper){
		String encLoginname = "";
		String encLoginpwd = "";
		try {
			encLoginname = Tool.encryptBASE64(bindPaper.getLoginName().getBytes("utf-8"));
			encLoginpwd = Tool.encryptBASE64(bindPaper.getEncPwd().getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		return encLoginname + "_" + encLoginpwd;
	}
	
	private String genRegPaperEncParam(BindEnt bindEnt){
		String encSzch = "";
		String encSqymc = "";
		String encMobile = "";
		try {
			encSzch = Tool.encryptBASE64(bindEnt.getSzch().getBytes("utf-8"));
			encSqymc = Tool.encryptBASE64(URLEncoder.encode(bindEnt.getSqymc(),"utf-8").getBytes("utf-8"));
			encMobile = Tool.encryptBASE64(bindEnt.getRegUser().getMobile().getBytes("utf-8"));
		}catch (UnsupportedEncodingException e) {
			log.error(e);
		}
		return encSzch + "_" + encSqymc + "_" + encMobile;
	}
	
	
	
}
